package com.esp.soccertutor.ui.xmlparser;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.esp.soccertutor.util.Log;
import com.esp.soccertutor.util.WebInterface;


public class XMLDocument {
	
	private static final String TAG = XMLDocument.class.getCanonicalName();
	
	String sourceURL;
	
	public static int HTTP_XML_STATUS = 0;
	
	public final static String getHttpPostXML(String wsURL,ArrayList<BasicNameValuePair> params) 
	{
		
		String xml = "";
		try {

			
			try {
				
				xml = WebInterface.doPost(wsURL,params);
				
				Log.print(TAG +" : getHttpPostXML()","[SERVER XML ] "+ xml);
						
				if (xml != null && !xml.equals("")) {
					
					HTTP_XML_STATUS = 200;

				} else {
					
					// No XML is available
					HTTP_XML_STATUS = 401;
				}
			} catch (Exception e) {
				
				HTTP_XML_STATUS = 500;
				Log.error(TAG+ " getHttpPostXML ", e);
			}

			

		} catch (Exception e) {
			
			HTTP_XML_STATUS = 500;
			Log.error(TAG+ " getHttpPostXML ", e);
		}

		return xml;
	}
	
	public final static String getHttpGetXML(String wsURL) 
	{
		
		String xml = "";
		try {

			
			try {
				
				xml = WebInterface.doGet(wsURL);
				
				Log.print(TAG +" : getHttpXML()","[SERVER XML ] "+ xml);
						
				if (xml != null && !xml.equals("")) {
					
					HTTP_XML_STATUS = 200;

				} else {
					
					// No XML is available
					HTTP_XML_STATUS = 401;
				}
			} catch (Exception e) {
				
				HTTP_XML_STATUS = 500;
				Log.error(TAG+"", e);
			}

			

		} catch (Exception e) {
			
			HTTP_XML_STATUS = 500;
			Log.error(TAG+"", e);
		}

		return xml;
	}
	
	public final static Document XMLfromString(String xml){

		
		Document doc = null;
		if(!xml.equals("") && xml != null){
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        try {

			DocumentBuilder db = dbf.newDocumentBuilder();
			
			InputSource is = new InputSource();
		    is.setCharacterStream(new StringReader(xml));
		    doc = db.parse(is); 
		    doc.getDocumentElement().normalize();
		    
			} catch (ParserConfigurationException e) {
				System.out.println("XML parse error: " + e.getMessage());
				return null;
				
			} catch (SAXException e) {
				System.out.println("Wrong XML file structure: " + e.getMessage());
	            return null;
	            
			} catch (IOException e) {
				System.out.println("I/O exeption: " + e.getMessage());
				return null;
				
			}
	
		}
		
        return doc;

		}
	public final static String getCharacterDataFromElement(Element e, String str) {
		Node child = e.getFirstChild();
		if (child instanceof CharacterData) {
			CharacterData cd = (CharacterData) child;
			
			return cd.getData();
		}
		return "";
	}
	public static int numResults(Document doc){		
		Node results = doc.getDocumentElement();
		int res = -1;
		
		try{
			res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
		}catch(Exception e ){
			res = -1;
		}
		
		return res;
	}
	public final static String getElementValue( Node elem ) {
	     Node kid;
	     if( elem != null){
	         if (elem.hasChildNodes()){
	             for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
	                 if( kid.getNodeType() == Node.TEXT_NODE  ){
	                     return kid.getNodeValue();
	                 }
	             }
	         }
	     }
	     return "";
	 }
	public static String getValue(Element item, String str) {		
		NodeList n = item.getElementsByTagName(str);		
		return XMLDocument.getElementValue(n.item(0));
	}	 

}
